package com.aps.apspda.dialog;

import android.app.Dialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatDialogFragment;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RadioButton;

import com.aps.apspda.R;
import com.aps.apspda.callback.StringCommonCallBack;
import com.aps.apspda.utils.StaticMembers;


/**
 * @author huangxin
 * @date 2015/11/10
 * @desc 通用dialog
 */
public class ChooseCheckFragmentDialog extends AppCompatDialogFragment {
    private String checkType = "DAY";
    private StringCommonCallBack callBack;

    public void setCallBack(StringCommonCallBack stringCommonCallBack) {
        this.callBack = stringCommonCallBack;
    }

    @Override
    public void show(FragmentManager manager, String tag) {
        if (!isAdded()) {
            FragmentTransaction ft = manager.beginTransaction();
            ft.add(this, tag);
            ft.commitAllowingStateLoss();
        }
    }

    @Override
    public void onStart() {
        super.onStart();
        Dialog dialog = getDialog();
        if (dialog != null) {
            dialog.getWindow().setLayout((int) (StaticMembers.SCREEN_WIDTH * 0.8), ViewGroup.LayoutParams.WRAP_CONTENT);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    public void dismissDialog() {
        dismiss();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_choose_check, null);
        getDialog().getWindow().setGravity(Gravity.CENTER); // 非常重要：设置对话框弹出的位置
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        getDialog().getWindow().setWindowAnimations(R.style.BottomDialog); // 添加动画
        getDialog().setCanceledOnTouchOutside(false);
        getDialog().setCancelable(false);
        getDialog().setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK) {
                    return true;
                }
                return false;
            }
        });
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        Button btnConfirm;
        RadioButton rbDay, rbWeek;

        rbDay = view.findViewById(R.id.rbDay);
        rbWeek = view.findViewById(R.id.rbWeek);
        rbDay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkType = "DAY";
            }
        });
        rbWeek.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkType = "WEEKS";
            }
        });
        btnConfirm = view.findViewById(R.id.btnConfirm);
        btnConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callBack.onCallback(checkType);
            }
        });
        super.onViewCreated(view, savedInstanceState);
    }
}
